/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lx_easyfinace;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Administrator
 */
public class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer {

    public TableCellTextAreaRenderer() {
        setLineWrap(true);
        setWrapStyleWord(true);
        this.setFont(new Font("宋体",0,16));
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        // 计算当下行的最佳高度
        int maxPreferredHeight = 0;
        for (int i = 0; i < table.getColumnCount(); i++) {

            float jine = 0;
            float payed = 0;
            //更改指定列的颜色

            try {
                jine = Float.parseFloat(table.getValueAt(row, 6).toString());
                payed = Float.parseFloat(table.getValueAt(row, 7).toString());
            } catch (java.lang.NumberFormatException e) {
                jine = 1;
            } catch (java.lang.ArrayIndexOutOfBoundsException e2) {
                jine =1;
            }
            if (jine < 0) {
                this.setBackground(new Color(30, 142, 255));
            } else if(payed > 0 && column == 7) {
                this.setBackground(new Color(30, 142, 255));
            } else {
                this.setBackground(Color.white);
            }
            


            Object o = table.getValueAt(row, i);
            setText("" + o);
            int w = table.getColumnModel().getColumn(column).getWidth();
            this.setSize(w, 0);
            int h = (int) getPreferredSize().getHeight();
            maxPreferredHeight = Math.max(maxPreferredHeight, h);
        }

        if (table.getRowHeight(row) != maxPreferredHeight && column == 3) // 少了这行则处理器瞎忙
        {
            table.setRowHeight(row, maxPreferredHeight);
        }

        setText(value == null ? "" : value.toString());
//        setAlignmentX(0.5f);
//        setAlignmentY(0.5f);
        return this;
    }
}
